﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ARR.Entities;
using ARR.UI.Models;
using System.Windows.Input;
using System.Collections.ObjectModel;
using Telerik.Windows.Controls;
using ARR.UI.Commands;
using ARR.Shared;
using Telerik.Windows.Controls.GridView;
using ARR.UI.Helpers;

namespace ARR.UI.ViewModels
{
    public class ArticleMasterChangeUomPopupViewModel : ViewModelBase
    {
        private IArticleMasterChangeUomPopupModel model;
        private ICommand selectionChangedCommand;
        private ICommand saveCommand;
        private ICommand cancelCommand;
        private ICommand addingNewDataItemCommand;


        public ArticleMasterChangeUomPopupViewModel(IArticleMasterChangeUomPopupModel model)
        {
            this.DisplayName = Labels.CambiarUomTitulo;
            this.model = model;
        }
        

        public ICommand AddingNewDataItemCommand
        {
            get
            {
                if (addingNewDataItemCommand == null)
                {
                    addingNewDataItemCommand = new RelayCommand(e => OnAddingNewRow(e as GridViewAddingNewEventArgs));
                }
                return addingNewDataItemCommand;
            }
        }
        
        public ICommand SaveCommand
        {
            get
            {
                if (saveCommand == null)
                {
                    saveCommand = new RelayCommand(e => Save(), x => SelectedUomConvertion != null);
                }
                return saveCommand;
            }
        }

        public ICommand CancelCommand
        {
            get
            {
                if (cancelCommand == null)
                {
                    cancelCommand = new RelayCommand(e => Cancel());
                }
                return cancelCommand;
            }
        }

        public ObservableCollection<UnitOfMeasureConvertion> UnitsOfMeasure
        {
            get
            {
                return model.UnitsOfMeasure;
            }
        }

        public UnitOfMeasureConvertion SelectedUomConvertion { get; set; }


        private void OnAddingNewRow(GridViewAddingNewEventArgs e)
        {
            e.NewObject = model.CreateNewUomConvertion();
        }
        
        private void Save()
        {
            if (AreValid())
            {
                model.Save(SelectedUomConvertion);

                WindowManager.CloseView(this, true);
            }
        }

        private void Cancel()
        {
            model.LoadUnitsOfMeasure();

            OnPropertyChanged<UnitsOfMeasureMasterViewModel>(p => p.UnitsOfMeasure);
        }

        private bool AreValid()
        {
            foreach (UnitOfMeasureConvertion uom in UnitsOfMeasure)
            {
                if (!uom.IsValid)
                    return false;
            }

            return true;
        }
        
    }
}
